from tkinter import Tk
from database.table import tablee
from database.table.tablee import Member
import tkinter as tk

def AdmendMember():
    admend_member_screen = Tk()
    admend_member_screen.title('修改会员信息')
    screen_width, screen_height = admend_member_screen.maxsize()
    # 窗口的宽和高
    width = 605
    height = 200
    centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                              (screen_height - height) / 2)
    admend_member_screen.geometry(centre)

    label = tk.Label(admend_member_screen, text='请输入要修改的会员卡号，填写要修改的信息：', font=('楷体', 14))
    label.place(x=110, y=15)

    # 卡号标签和文本框
    user_card_label = tk.Label(admend_member_screen, text='卡号', font=('楷体', 14))
    user_card_label.place(x=110, y=60)
    user_card_text = tk.StringVar()
    user_card_entry = tk.Entry(admend_member_screen, textvariable=user_card_text,
                               width=15)
    user_card_entry.place(x=160, y=60)
    # 姓名标签和文本框
    user_name_label = tk.Label(admend_member_screen, text='姓名', font=('楷体', 14))
    user_name_label.place(x=340, y=60)
    user_name_text = tk.StringVar()
    user_name_entry = tk.Entry(admend_member_screen, textvariable=user_name_text,
                               width=15)
    user_name_entry.place(x=390, y=60)
    # 电话标签和文本框
    user_phone_label = tk.Label(admend_member_screen, text='电话', font=('楷体', 14))
    user_phone_label.place(x=110, y=115)
    user_phone_text = tk.StringVar()
    user_phone_entry = tk.Entry(admend_member_screen, textvariable=user_phone_text,
                                width=15)
    user_phone_entry.place(x=160, y=115)
    # 积分标签和文本框
    user_point_label = tk.Label(admend_member_screen, text='积分', font=('楷体', 14))
    user_point_label.place(x=340, y=115)
    user_point_text = tk.StringVar()
    user_point_entry = tk.Entry(admend_member_screen, textvariable=user_point_text,
                                width=15)
    user_point_entry.place(x=390, y=115)

    # 确定按钮响应事件
    def confirm():
        id = str(user_card_entry.get())
        memberr = tablee.session.query(Member).filter(Member.Mcardnum == id).all()
        data = Member.to_json(memberr)
        length = len(data)

        if length == 1:  # 修改编号存在

            name = str(user_name_entry.get())
            phone = str(user_phone_entry.get())
            point = str(user_point_entry.get())
            if name != '':
                members = Member(Mcardnum=id, Mname=name)
                tablee.session.merge(members)
                tablee.session.commit()
            if point != '':
                members = Member(Mcardnum=id, Mpoint=int(point))
                tablee.session.merge(members)
                tablee.session.commit()
            if phone != '':
                members = Member(Mcardnum=id, Mphone=phone)
                tablee.session.merge(members)
                tablee.session.commit()

            tablee.session.close()

            admend = tk.Toplevel(admend_member_screen)
            admend.title("Success")
            screen_width, screen_height = admend.maxsize()
            # 窗口的宽和高
            width = 240
            height = 100
            centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                                      (screen_height - height) / 2)
            admend.geometry(centre)
            label = tk.Label(admend, text='修改成功！', font=('楷体', 14))
            label.place(x=60, y=15)

            def confirmm():
                admend.destroy()

            # 确定按钮
            user_confirm_button = tk.Button(admend, text='确定', font=('楷体', 12),
                                            width=10, command=confirmm)
            user_confirm_button.place(x=65, y=60)

        elif id == '':  # 卡号为空
            admend = tk.Toplevel(admend_member_screen)
            admend.title("Error")
            screen_width, screen_height = admend.maxsize()
            # 窗口的宽和高
            width = 400
            height = 100
            centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                                      (screen_height - height) / 2)
            admend.geometry(centre)
            label = tk.Label(admend, text='卡号不能为空，请重新输入！', font=('楷体', 14))
            label.place(x=60, y=15)

            def confirmm():
                admend.destroy()

            # 确定按钮
            user_confirm_button = tk.Button(admend, text='确定', font=('楷体', 12),
                                            width=10, command=confirmm)
            user_confirm_button.place(x=140, y=60)

        else:  # 卡号不存在
            admend = tk.Toplevel(admend_member_screen)
            admend.title("Error")
            screen_width, screen_height = admend.maxsize()
            # 窗口的宽和高
            width = 400
            height = 100
            centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                                      (screen_height - height) / 2)
            admend.geometry(centre)
            label = tk.Label(admend, text='此卡号不存在,请重新输入！', font=('楷体', 14))
            label.place(x=60, y=15)

            def confirmm():
                admend.destroy()

            # 确定按钮
            user_confirm_button = tk.Button(admend, text='确定', font=('楷体', 12),
                                            width=10, command=confirmm)
            user_confirm_button.place(x=140, y=60)

    # 返回按钮响应事件
    def back():
        admend_member_screen.destroy()

    # 确定按钮
    user_confirm_button = tk.Button(admend_member_screen, text='确定', font=('楷体', 12),
                                    width=10, command=confirm)
    user_confirm_button.place(x=160, y=160)
    # 返回按钮
    user_back_button = tk.Button(admend_member_screen, text='返回', font=('楷体', 12),
                                 width=10, command=back)
    user_back_button.place(x=390, y=160)

    admend_member_screen.mainloop()

if __name__=='__main__':
    AdmendMember()